package com.xfly.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.utils.GlideUtil;
import com.xfly.app.bean.CarBean;
import com.xfly.app.bean.KcBean;

import java.util.List;


/**
 * Created by Administrator on 2018/1/12.
 */



public class JhCarAdapter extends BaseAdapter {

    private Context mContext;
    private List<CarBean.GoodsBean> mDataList;
    private LayoutInflater mLayoutInflater;

    public JhCarAdapter(Context context, List<CarBean.GoodsBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<CarBean.GoodsBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }

    Click click;

    public void setClick(Click click){
        this.click = click;
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_jhcargood, parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        CarBean.GoodsBean bean = mDataList.get(position);

        viewHolder.title.setText(bean.getSpecs_name());
        viewHolder.ys.setText("已售："+bean.getSale());
        viewHolder.kc.setText("库存："+bean.getStock());
        viewHolder.shopcar_num.setText(bean.getNum()+"");
        viewHolder.price.setText("￥"+bean.getPrice());
        GlideUtil.showImg(mContext,bean.getGoods_cover(),viewHolder.logo);

        viewHolder.shopcar_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click.Click("+",bean.getSpecs_id()+"");
            }
        });

        viewHolder.shopcar_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                click.Click("-",bean.getId()+"");
            }
        });

        return convertView;
    }

    class ViewHolder {

        TextView title,ys,kc,price,sxj,shopcar_num;
        ImageView logo;
        RelativeLayout shopcar_jian,shopcar_jia;

        ViewHolder(View view) {
            sxj = view.findViewById(R.id.sxj);
            title = (TextView) view.findViewById(R.id.title);
            price = (TextView) view.findViewById(R.id.price);
            ys = (TextView) view.findViewById(R.id.ys);
            kc = (TextView) view.findViewById(R.id.kc);
            logo = (ImageView) view.findViewById(R.id.logo);
            shopcar_jian = view.findViewById(R.id.shopcar_jian);
            shopcar_jia = view.findViewById(R.id.shopcar_jia);
            shopcar_num = view.findViewById(R.id.shopcar_num);
        }
    }

    public  interface Click{
        void Click(String status,String id);
        void Click2(int position);
    }

}
